package com.ks.app.search.action;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.ks.app.admin.action.BaseAction;
import com.ks.app.admin.service.KsVersionService;
import com.ks.app.navigate.service.KsNavigateService;
import com.ks.app.navigate.util.JsonUtil;
import com.ks.app.search.util.Constants;

@ParentPackage("struts-default")
@InterceptorRef("defaultStack")
@Results( {
	@Result(name = "editnav", location = "/WEB-INF/navigate/editnav.jsp"),
	@Result(name = "listnav", location = "/WEB-INF/navigate/listnav.jsp")
})
public class KsUpdateAction extends BaseAction{
	

	private static final long serialVersionUID = 3118435042447210456L;
	private Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="ksNavigateService")
	private KsNavigateService navService;
	
	@Resource(name="ksVersionService")
	private KsVersionService verService;


	/**
	 * kso客户端登陆更新接口之 搜索导航，站点导航
	 * @return
	 */
	@Action(value="ksoupdate")
	public String ksoupdate(){
		
		long    wv = request.getParameter("wv") ==null?0l:Long.parseLong(request.getParameter("wv"));   //客户端站点导航版本号
		long   adv = request.getParameter("adv")==null?0l:Long.parseLong(request.getParameter("adv"));  //客户端广告墙版本号
		String apk = request.getParameter("apk")==null?"":request.getParameter("apk");                  //客户端版本号
		
		long curr_wv    = Long.parseLong(verService.findVersion(2, 2));          //站点导航版本号
		long curr_adv   = Long.parseLong(verService.findVersion(3, 2));          //广告
		String  curr_apk = verService.findVersion(1, 2);                         //apk
		
		Map<String,Object> update = new HashMap<String,Object>();
		
		if(!apk.equals(curr_apk)){
			
			Map<String,Object> apkmap = new HashMap<String,Object>();
			apkmap.put("ver", curr_apk);
			update.put("apk", apkmap);
			
		}else{
			
			String snav = Constants.CONS_PROPERTIES.getValue("snav");
			update.put("snav", snav);
			
			if(wv < curr_wv){
				Map<String,Object>  wmap = navService.findNavigateWebsite();
				wmap.put("ver", curr_wv);
				update.put("wnav",wmap);
			}
			
			if(adv < curr_adv){
				
				Map<String,Object> advmap = new HashMap<String,Object>();
				advmap.put("ver", curr_adv);
				update.put("adv", advmap);
			}
		}
		
		try {
			JsonUtil.outJson(update, response);
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
			JsonUtil.outJson("{\"error\":"+e.getMessage()+"}", response);
		}
		
		return null;
	}
	


}
